<?php
/*
*	Template Name: Project Search
*/

?>

<?php get_header(); ?>
<div class="container" id="top_info">
	<div class="top-content">&nbsp;</div>
    <div class="top-curve">&nbsp;</div>
</div>

<div class="container">
	<div class="page-filler">&nbsp;</div>
	
 	<div class="page-content padded">
		<?php
			$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
			$args = array(
					'post_type' => 'dmci_projects',
					'paged' => $paged,
					'orderby' => 'title',
					'posts_per_page' => 9,
					'order' => 'ASC'
				);
			if( isset($_GET['project_type']) ){
				$args['dmci_project_categories'] = $_GET['project_type'];
			}
			if( isset($_GET['project_type']) ){
				$args['dmci_project_locations'] = $_GET['project_location'];
			}
			$query = new WP_Query( $args );
			$posts = $query->found_posts;
		?>
		<div class="search-form-container">
			<?php
				$proj_type = get_term_by('slug', $_GET['project_type'], 'dmci_project_categories');
				$proj_location = get_term_by('slug', $_GET['project_location'], 'dmci_project_locations');
				$search_terms = '';
				$search_terms .= isset($proj_type->name) ? $proj_type->name . ', ' : '';
				$search_terms .= isset($proj_location->name) ? $proj_location->name : '';
			
				if( ( isset($project_type->name) && $proj_type->name == '' ) || ( isset($proj_location->name) && $proj_location->name == '' )){
					$search_terms = str_replace(', ', '', $search_terms);
				}
				
				if( !$query->have_posts() ) {
					echo '<div class="result-notice">No results found for "'.$search_terms.'"</div>';
				}
				else if ( ( isset( $proj_type->name ) && $proj_type->name != '' ) || ( isset($proj_location->name ) && $proj_location->name != '' ) ){
					echo '<div class="result-notice"> '. $posts .' results for "' . $search_terms.'"</div>';
				}
			?>
		
		</div>
		<ul class="search-list">
		<?php 
			
		while ( $query->have_posts() ) :
			$query->the_post();
			$proj_category = wp_get_post_terms(get_the_ID(), 'dmci_project_categories', array('fields' => 'names') ); 
			$proj_location = wp_get_post_terms(get_the_ID(), 'dmci_project_locations', array('fields' => 'names') );
			$sliders = get_post_meta($post->ID, 'dmci_sliders', true);
			$proj_title = get_the_title();
			?>
			
			<li>
				<a href="<?php echo get_permalink() ?>" class="search-img">
					<?php if ( !empty($sliders) ): $sliders = json_decode(htmlspecialchars_decode($sliders), true); ?>
					<img src="<?php echo $sliders[0] ?>" alt="<?php echo $proj_title ?> Image" />
					<?php endif;?>
				</a>
				<div class="title"><?php the_title(); ?></div>
				<div class="category"><?php echo isset($proj_category[0]) ? $proj_category[0] : '&nbsp;';?></div>
				<div class="location"><?php echo isset($proj_location[0]) ? $proj_location[0] : '&nbsp;'?></div>
				<a href="<?php echo get_permalink(); ?>" class="explore"><span class="arrow">></span>Explore Project</a>
			</li>
		
		<?php
			endwhile;
		?>
		 </ul>
		 <div class="pagination">
		<?php
			$big = 999999999; // need an unlikely integer

			echo paginate_links( array(
				'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
				'format' => '?paged=%#%',
				'current' => max( 1, get_query_var('paged') ),
				'total' => $query->max_num_pages
			) );
		?>
 		</div>
 	</div>
</div>
<?php get_footer(); ?>